package com.micro.school.order.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.micro.school.common.core.context.SecurityContextHolder;
import com.micro.school.common.core.domain.R;
import com.micro.school.common.core.utils.SecurityContextHolderUtils;
import com.micro.school.order.entity.PayPrepaymentProject;
import com.micro.school.order.entity.PayStudentPrepayment;
import com.micro.school.order.mapper.PayStudentPrepaymentMapper;
import com.micro.school.order.service.PayPrepaymentProjectService;
import com.micro.school.order.service.PayStudentPrepaymentService;
import com.micro.school.system.api.RemoteEduService;
import com.micro.school.system.api.RemoteUserService;
import com.micro.school.system.api.domain.StudentInfos;
import com.micro.school.system.api.domain.vo.*;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service("PayStudentPrepaymentServiceImpl")
public class PayStudentPrepaymentServiceImpl implements PayStudentPrepaymentService {

    @Resource
    private PayStudentPrepaymentMapper payStudentPrepaymentMapper;

    @Resource
    private RemoteEduService remoteEduService;

    @Resource
    private RemoteUserService remoteUserService;

    @Resource
    private PayPrepaymentProjectService payPrepaymentProjectService;

    @Override
    public List<PayStudentPrepayment> queryAllTable(PayStudentPrepayment payStudentPrepayment) {
        List<PayStudentPrepayment> payStudentPrepayments = payStudentPrepaymentMapper.queryAllByLimit(payStudentPrepayment);
        if (SecurityContextHolder.getUserIdcardState() != null){
            if (1 == SecurityContextHolder.getUserIdcardState()){
                payStudentPrepayments.forEach(vo -> {
                    if (vo.getIdNumber() != null && vo.getIdNumber().length() > 8) {
                        String maskedIdNumber = vo.getIdNumber().substring(0, vo.getIdNumber().length() - 8) + "********";
                        vo.setIdNumber(maskedIdNumber);
                    } else if (vo.getIdNumber() != null) {
                        vo.setIdNumber(vo.getIdNumber().replaceAll(".", "*"));
                    }
                });
            }
        }
        return payStudentPrepayments;
    }

    @Override
    public void insertOne(PayStudentPrepayment payStudentPrepayment) {
        payStudentPrepayment.setCreateBy(SecurityContextHolder.getUserName());
        payStudentPrepayment.setUpdateBy(SecurityContextHolder.getUserName());
        payStudentPrepayment.setCreateTime(new Date());
        payStudentPrepayment.setUpdateTime(new Date());

        SchoolCollegeVos schoolCollegeVos = new SchoolCollegeVos();
        Long collegeId = payStudentPrepayment.getCollegeId();
        schoolCollegeVos.setId(collegeId);
        schoolCollegeVos.setDeleteFlag("0");
        R<List<SchoolCollegeVos>> listR = remoteEduService.checkCollege(schoolCollegeVos);
        if (listR.getData() != null && listR.getData().size() > 0) {
            payStudentPrepayment.setCollegeName(String.valueOf(listR.getData().get(0).getCollegeName()));
            payStudentPrepayment.setCampusId(listR.getData().get(0).getCampusId());
        }

        if (listR.getData().get(0) != null){
            SchoolCampusVos schoolCampusVos = new SchoolCampusVos();
            schoolCampusVos.setId(listR.getData().get(0).getCampusId());
            schoolCampusVos.setDeleteFlag("0");
            schoolCampusVos.setStatus("1");
            R<List<SchoolCampusVos>> listR1 = remoteEduService.checkCampus(schoolCampusVos);
            if (listR1.getData() != null && listR1.getData().size() > 0) {
                payStudentPrepayment.setCampusName(listR1.getData().get(0).getCampusName());
            }
        }

        StudentInfoVos studentInfoVos = new StudentInfoVos();
        studentInfoVos.setName(payStudentPrepayment.getStudentName());
        studentInfoVos.setIdNumber(payStudentPrepayment.getIdNumber());
        studentInfoVos.setIsDel(0);
        R<StudentInfos> studentInfosR = remoteUserService.queryStudent(studentInfoVos);
        if (studentInfosR != null) {
            payStudentPrepayment.setStudentId(Math.toIntExact(studentInfosR.getData().getId()));
        }

        SchoolFeeYearVos schoolFeeYearVos = new SchoolFeeYearVos();
        schoolFeeYearVos.setDeleteFlag("0");
        schoolFeeYearVos.setTenantId(Long.valueOf(SecurityContextHolder.getUserTenantId()));
        schoolFeeYearVos.setStatus("1");
        schoolFeeYearVos.setIsCloseChannel("1");
        schoolFeeYearVos.setId(Long.valueOf(payStudentPrepayment.getFeeYearId()));
        R<List<SchoolFeeYearVos>> listR3 = remoteEduService.checkFeeYear(schoolFeeYearVos);
        if (listR3.getData() != null && listR3.getData().size() > 0) {
            payStudentPrepayment.setFeeYearId(Math.toIntExact(listR3.getData().get(0).getId()));
            payStudentPrepayment.setFeeYear(listR3.getData().get(0).getFeeYear());
        }

        CollegeModelVos collegeModelVos = new CollegeModelVos();
        collegeModelVos.setId(payStudentPrepayment.getModelId());
        collegeModelVos.setDeleteFlag("0");
        collegeModelVos.setStatus("1");
        R<List<CollegeModelVos>> listR1 = remoteEduService.checkEducation(collegeModelVos);
        if (listR1.getData() != null && listR1.getData().size() > 0) {
            payStudentPrepayment.setModelName(String.valueOf(listR1.getData().get(0).getModelName()));
        }

        CollegeMajorVos collegeMajorVos = new CollegeMajorVos();
        collegeMajorVos.setId(payStudentPrepayment.getMajorId());
        collegeMajorVos.setDeleteFlag("0");
        collegeMajorVos.setStatus("1");
        R<List<CollegeMajorVos>> listR2 = remoteEduService.checkMajor(collegeMajorVos);
        if (listR2.getData() != null && listR2.getData().size() > 0) {
            payStudentPrepayment.setMajorName(listR2.getData().get(0).getMajorName());
        }

        SchoolGradeVos schoolGradeVos = new SchoolGradeVos();
        schoolGradeVos.setId(payStudentPrepayment.getGradeId());
        schoolGradeVos.setDeleteFlag("0");
        schoolGradeVos.setStatus("1");
        R<List<SchoolGradeVos>> listGradeVo = remoteEduService.checkGrade(schoolGradeVos);
        if (listGradeVo.getData() != null && listGradeVo.getData().size() > 0) {
            payStudentPrepayment.setGradeName(listGradeVo.getData().get(0).getGrade());
        }

        PayPrepaymentProject payPrepaymentProject = new PayPrepaymentProject();
        payPrepaymentProject.setId(payStudentPrepayment.getSubjectId());
        payPrepaymentProject.setDeleteFlag("0");
        List<PayPrepaymentProject> payPrepaymentProjectList = payPrepaymentProjectService.queryList(payPrepaymentProject);
        if (payPrepaymentProjectList != null && payPrepaymentProjectList.size() > 0) {
            payStudentPrepayment.setProjectName(payPrepaymentProjectList.get(0).getProjectName());
        }
        payStudentPrepayment.setPaymentStatus(10);
        payStudentPrepayment.setCollectionStatus(10);
        payStudentPrepayment.setDeleteFlag("0");
        payStudentPrepayment.setTenantId(Integer.valueOf(SecurityContextHolder.getUserTenantId()));
        payStudentPrepayment.setIsCancel(0);
        payStudentPrepayment.setDeleteFlag("0");
        payStudentPrepayment.setDeductionAmount(BigDecimal.ZERO);
        payStudentPrepaymentMapper.insert(payStudentPrepayment);
    }

    @Override
    public List<PayStudentPrepayment> getByIdNumber(String idNumber) {
        String userTenantId = SecurityContextHolder.getUserTenantId();
        return payStudentPrepaymentMapper.getByIdNumber(idNumber,userTenantId);
    }

    @Override
    public void cancel(Integer id) {
        String userTenantId = SecurityContextHolder.getUserTenantId();
        payStudentPrepaymentMapper.cancel(id,userTenantId);
    }

    @Override
    public void delete(Integer id) {
        String userTenantId = SecurityContextHolder.getUserTenantId();
        payStudentPrepaymentMapper.delete(id,userTenantId);
    }

    @Override
    public List<PayStudentPrepaymentVos> getStudentPrepaymentVos(PayStudentPrepaymentVos payStudentPrepaymentVos) {
        return payStudentPrepaymentMapper.getStudentPrepaymentVos(payStudentPrepaymentVos);
    }

    @Override
    public void updateCollectionStatus(PayStudentPrepaymentVos payStudentPrepaymentVos) {
        payStudentPrepaymentMapper.updateCollectionStatus(payStudentPrepaymentVos);
    }

    @Override
    public void updateStudentPrepayment(PayStudentPrepaymentVos payStudentPrepaymentVos) {
        payStudentPrepaymentMapper.updateStudentPrepayment(payStudentPrepaymentVos);
    }

    @Override
    public List<PayStudentPrepaymentVos> getStudentPrepaymentNoVos(PayStudentPrepaymentVos payStudentPrepaymentVos) {
        return payStudentPrepaymentMapper.getStudentPrepaymentNoVos(payStudentPrepaymentVos);
    }

}
